package com.kinyx.framework.db.vo;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.db.configs.NamingStrategy;
import com.kinyx.framework.db.defines.INamingStrategy;
import com.kinyx.framework.enums.impl.ErrorCode1;
import com.kinyx.framework.exception.KRuntimeException;

public class MappingHandler_Tester_load_table_name {

	private final INamingStrategy ns = new NamingStrategy();

	public class No {}

	@Test
	public void test1() {
		Assert.assertEquals("T_NO", MappingHandler.load_table_name(No.class, this.ns));
	}

	@Entity(name = "")
	public class EntityBlank {}

	@Entity(name = "foo")
	public class _Entity {}

	@Test
	public void test2() {
		Assert.assertEquals("T_ENTITY_BLANK", MappingHandler.load_table_name(EntityBlank.class, this.ns));
		Assert.assertEquals("foo", MappingHandler.load_table_name(_Entity.class, this.ns));
	}

	@Table(name = "")
	public class TableBlank {}

	@Table(name = "foo")
	public class _Table {}

	@Test
	public void test3() {
		Assert.assertEquals("T_TABLE_BLANK", MappingHandler.load_table_name(TableBlank.class, this.ns));
		Assert.assertEquals("foo", MappingHandler.load_table_name(_Table.class, this.ns));
	}

	@org.hibernate.annotations.Table(appliesTo = "")
	public class HTableBlank {}

	@org.hibernate.annotations.Table(appliesTo = "foo")
	public class _HTable {}

	@Test
	public void test4() {
		Assert.assertEquals("T_H_TABLE_BLANK", MappingHandler.load_table_name(HTableBlank.class, this.ns));
		Assert.assertEquals("foo", MappingHandler.load_table_name(_HTable.class, this.ns));
	}

	@Entity(name = "foo")
	@Table(name = "bar")
	public class _Error1 {}

	@Test
	public void testError1() {
		Assert.assertThrows(Exception.class, () -> {
			try {
				MappingHandler.load_table_name(_Error1.class, this.ns);
			} catch (final Exception e) {
				Assert.assertTrue(e instanceof KRuntimeException);
				Assert.assertEquals(ErrorCode1.CODE_ERROR_41, ((KRuntimeException) e).getErrorCode());
				System.out.println(e.getMessage());
				throw e;
			}
		});
	}

	@Entity(name = "foo")
	@org.hibernate.annotations.Table(appliesTo = "bar")
	public class _Error2 {}

	@Test
	public void testError2() {
		Assert.assertThrows(Exception.class, () -> {
			try {
				MappingHandler.load_table_name(_Error2.class, this.ns);
			} catch (final Exception e) {
				Assert.assertTrue(e instanceof KRuntimeException);
				Assert.assertEquals(ErrorCode1.CODE_ERROR_42, ((KRuntimeException) e).getErrorCode());
				System.out.println(e.getMessage());
				throw e;
			}
		});
	}

	@Table(name = "foo")
	@org.hibernate.annotations.Table(appliesTo = "bar")
	public class _Error3 {}

	@Test
	public void testError3() {
		Assert.assertThrows(Exception.class, () -> {
			try {
				MappingHandler.load_table_name(_Error3.class, this.ns);
			} catch (final Exception e) {
				Assert.assertTrue(e instanceof KRuntimeException);
				Assert.assertEquals(ErrorCode1.CODE_ERROR_43, ((KRuntimeException) e).getErrorCode());
				System.out.println(e.getMessage());
				throw e;
			}
		});
	}

	@Entity(name = "foo")
	@Table(name = "foo")
	@org.hibernate.annotations.Table(appliesTo = "foo")
	public class Success {}

	@Test
	public void test() {
		Assert.assertEquals("foo", MappingHandler.load_table_name(Success.class, this.ns));
	}

}
